package com.laicunba.service.activity.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.laicunba.constant.FinancingTargetConstant;
import com.laicunba.pojo.FinancingTargetBidDO;
import com.laicunba.pojo.TemplateDO;
import com.laicunba.pojo.UserDO;
import com.laicunba.service.MobileMessageService;
import com.laicunba.service.RegistryService;
import com.laicunba.service.TemplateService;
import com.laicunba.service.accounting.RewardBidService;
import com.laicunba.service.activity.FriendBidActivityService;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Service
public class FriendBidActivityServiceImpl implements FriendBidActivityService {
	@Autowired
	private RegistryService registryService;
	@Autowired
	private RewardBidService rewardBidService;
	@Autowired
	private MobileMessageService mobileMessageService;
	@Autowired
	private TemplateService templateService;

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Transactional
	@Override
	public boolean doActivity(UserDO userDO, UserDO inviteUser, FinancingTargetBidDO bidDO) {

		String id = "u." + userDO.getId() + ".invite.reward.";
		Map<String, String> map = registryService.likeKey(id);
		Map rewardMsgParam = new HashMap();
		rewardMsgParam.put("MobileShortNo", StringUtil.endSubstring(userDO.getMobile(), 4));

		RewardBidService.BuyParam buyParam = new RewardBidService.BuyParam();
		buyParam.setUserId(inviteUser.getId());
		buyParam.setInviteUser(userDO);
		// 3.好友购买10000元及以上，理财期限1个月及以上的理财产品
		if (bidDO.getBuyAmount().compareTo(BigDecimal.valueOf(10000)) >= 0 && (bidDO.getBeginProfit() != null && bidDO.getEndProfit() != null) && DateUtil.days(bidDO.getBeginProfit(), bidDO.getEndProfit()) >= 25) {
			if (!map.containsKey(id.concat("10000"))) {
				buyParam.setBuyAmount(BigDecimal.valueOf(50000));
				buyParam.setTag(FinancingTargetConstant.TAG_INVITE_10000);
				buyParam.setRegistryId(id + "10000");
				rewardMsgParam.put("buyAmount", "10000");
				rewardMsgParam.put("rewardAmount", buyParam.getBuyAmount().toString());
				rewardBidService.doBuy(buyParam);
				postMessage(inviteUser, rewardMsgParam);
				return true;
			}
		}

		// 2.5000元及以上10000元以下
		if (bidDO.getBuyAmount().compareTo(BigDecimal.valueOf(5000)) >= 0 && bidDO.getBuyAmount().compareTo(BigDecimal.valueOf(10000)) <= 0 && (bidDO.getBeginProfit() != null && bidDO.getEndProfit() != null) && DateUtil.days(bidDO.getBeginProfit(), bidDO.getEndProfit()) >= 25) {
			if (!map.containsKey(id.concat("MORE_5000"))) {
				buyParam.setBuyAmount(BigDecimal.valueOf(30000));
				buyParam.setTag(FinancingTargetConstant.TAG_INVITE_MORE_5000);
				buyParam.setRegistryId(id.concat("MORE_5000"));
				rewardMsgParam.put("buyAmount", "5000");
				rewardMsgParam.put("rewardAmount", buyParam.getBuyAmount().toString());
				rewardBidService.doBuy(buyParam);
				postMessage(inviteUser, rewardMsgParam);
				return true;
			}
		}

		// 1.好友购买任意金额下定期理财产品
		if (!map.containsKey(id.concat("LESS_5000"))&&bidDO.getBuyAmount().compareTo(BigDecimal.valueOf(1000)) >= 0 && (bidDO.getBeginProfit() != null && bidDO.getEndProfit() != null) && DateUtil.days(bidDO.getBeginProfit(), bidDO.getEndProfit()) >= 25) {
			buyParam.setBuyAmount(BigDecimal.valueOf(20000));
			buyParam.setTag(FinancingTargetConstant.TAG_INVITE_LESS_5000);
			buyParam.setRegistryId(id.concat("LESS_5000"));
			rewardMsgParam.put("buyAmount", "100");
			rewardMsgParam.put("rewardAmount", buyParam.getBuyAmount().toString());
			rewardBidService.doBuy(buyParam);
			postMessage(inviteUser, rewardMsgParam);
		}
		return true;
	}

	@SuppressWarnings("rawtypes")
	private void postMessage(UserDO inviteUser, Map rewardMsgParam) {
		TemplateDO rewardMsg = templateService.findByCodeWidthDefault("MOBILE_MSG_INVITE_FIRSTBUY");
		mobileMessageService.sendMsgAsync(inviteUser.getMobile(), templateService.simpleMerge(rewardMsg.getContent(), rewardMsgParam));
	}

}
